import { getConfigInfo } from "@/util/ConfigInfo"
import { getCodeCallbackUrl, tokenFilePath } from "@/util/net-disk/baidu/func";
import { redirect } from "next/navigation";
import * as nodeFileSys from "fs";

export async function GET() {
  if (nodeFileSys.existsSync(tokenFilePath)) {
    return Response.json({
      success: true,
      hasToken: true,
    });
  }
  const data = await getConfigInfo();
  const baiduConfig = data.netDisk.Baidu!;
  let url = "http://openapi.baidu.com/oauth/2.0/authorize" +
  "?response_type=code" + 
  `&client_id=${baiduConfig.appKey}` +
  `&redirect_uri=${encodeURIComponent(getCodeCallbackUrl())}` +
  `&scope=basic,netdisk` + 
  `&device_id=${baiduConfig.appName}`;
  return redirect(url);
}